package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 检查祖服务
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl  implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;


    //新增检查祖,同时还需要检查祖关联检查项
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //新增检查祖
        checkGroupDao.add(checkGroup);
        //设置检查祖与检查项关联
        Integer checkGroupId = checkGroup.getId();
        setCheckGroupAndCheckItem(checkGroupId,checkitemIds);
    }
    //分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//查询条件

        //分页助手插件
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> checkGroups= checkGroupDao.selectByCondition(queryString);

        long total = checkGroups.getTotal();
        List<CheckGroup> rows = checkGroups.getResult();
        PageResult pageResult = new PageResult(total, rows);
        return pageResult;

    }
    //查询检查祖信息
    @Override
    public CheckGroup findById(Integer checkgroupId) {
        CheckGroup checkGroup=checkGroupDao.findById(checkgroupId);
        return checkGroup;
    }
    //编辑检查祖的检查项回显数据
    @Override
    public List<Integer> findCheckitemIdsByCheckgroupId(Integer id) {

        List<Integer> checkitemids= checkGroupDao.findCheckitemIdsByCheckgroupId(id);
        return checkitemids;
    }
    //编辑检查祖信息
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //修改检查祖基本信息,操作检查祖t_checkgroup表
        checkGroupDao.edit(checkGroup);
        //清理当前检查组的关联的检查项,操作中间关系表t_checkgroup_checkitem表
        checkGroupDao.deleteAssoication(checkGroup.getId());

        Integer checkGroupId = checkGroup.getId();
        //重新建立当前检查组和检查项的当前关联
        setCheckGroupAndCheckItem(checkGroupId,checkitemIds);
    }
    //删除检查组
    @Override
    public void deleteById(Integer id) {
        //判断检查项是否有外键关联
        long count = checkGroupDao.findCountByCheckGroupId(id);
        if(count>0){
            //存在外键关联,不允许删除
            new RuntimeException();
        }
        checkGroupDao.deleteById(id);
    }
    //查询所有检查组
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    //建立检查组和检查项多对多关系(抽取代码)
    public void  setCheckGroupAndCheckItem(Integer checkGroupId,Integer[] checkitemIds){

        if(checkitemIds != null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer> map= new HashMap<>();
                map.put("checkgroupId",checkGroupId);
                map.put("checkitemId",checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }
}
